package com.example.zzm.yuejian03.Model;


import com.example.zzm.yuejian03.Bean.collectBean;
import com.example.zzm.yuejian03.Iface.CollectclubIface;
import com.example.zzm.yuejian03.Listener.CollectclubListener;
import com.example.zzm.yuejian03.Listener.TListener;
import com.example.zzm.yuejian03.Server;
import com.example.zzm.yuejian03.Service.Service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class CollectclubModel implements CollectclubIface {

    private Retrofit retrofit;

    //构造函数
    public CollectclubModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(Server.BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    //收藏
    @Override
    public void Like(int club_id,int user_id,final TListener listener) {
        Service service
                =retrofit.create(Service.class);
        Call<collectBean> call
                =service.getGuanzhu(club_id,user_id);
        call.enqueue(new Callback<collectBean>() {
            @Override
            public void onResponse(Call<collectBean> call, Response<collectBean> response) {
                if(response.isSuccessful() && response!=null)
                {  listener.onResponse(response.body());
                }
                else   listener.onFail("on response fail");
            }
            @Override
            public void onFailure(Call<collectBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }
    //取消收藏
    @Override
    public void unLike(int club_id,int user_id,final TListener listener) {
        Service service
                =retrofit.create(Service.class);
        Call<collectBean> call
                =service.getQuxiao(club_id,user_id);


        call.enqueue(new Callback<collectBean>() {
            @Override
            public void onResponse(Call<collectBean> call, Response<collectBean> response) {
                if(response.isSuccessful() && response!=null)
                {  listener.onResponse(response.body());
                }
                else   listener.onFail("on response fail");
            }
            @Override
            public void onFailure(Call<collectBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }
    //判断是否收藏
    @Override
    public void islike(int club_id,int user_id,final TListener listener) {
        Service service
                =retrofit.create(Service.class);
        Call<collectBean> call
                =service.getIslike(club_id,user_id);
        call.enqueue(new Callback<collectBean>() {
            @Override
            public void onResponse(Call<collectBean> call, Response<collectBean> response) {
                if(response.isSuccessful() && response!=null)
                {  listener.onResponse(response.body());
                }
                else   listener.onFail("on response fail");
            }
            @Override
            public void onFailure(Call<collectBean> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });

    }

}
